-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph implementation using Incidence Matrix.cpp
50 lines (46 loc) · 1.37 KB
/
Graph implementation using Incidence Matrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <vector>
using namespace std;
class Graph {
private:
int m = 0, edge = 0;
vector<vector<int>> matrix;
public:
Graph(int nodes, int edges){ // Constructor
m = nodes;
matrix = vector<vector<int>>(m, vector<int>(edges));
}
void addEdge(int a, int b){
if(a >= m || b >= m){
cout << "Invalid edge [" << a << "]🠪[" << b << "]" << endl << "Node value should be max (m-1)" << endl << endl;
} else if(edge == matrix[0].size()){
cout << "Edge [" << a << "]🠪[" << b << "] not being " << (a == b ? "looped." : "connected.") << endl << "Maxmimum number of edge(s) has been reached!" << endl << endl;
} else {
matrix[a][edge] = 1;
if(a != b) matrix[b][edge] = -1;
edge++;
}
}
void print(){
for (int row = 0; row < matrix.size(); row++){
for (int col = 0; col < matrix[row].size(); col++){
cout << (matrix[row][col] == -1 ? " " : " ") << matrix[row][col];
}
cout << endl;
}
}
};
int main(){
int m = 6, edges = 8;
Graph G(m, edges);
G.addEdge(0, 2);
G.addEdge(2, 3);
G.addEdge(3, 4);
G.addEdge(4, 5);
G.addEdge(4, 1);
G.addEdge(1, 0);
G.addEdge(0, 4);
G.addEdge(2, 5);
cout << "Incidence Matrix: " << endl;
G.print();
}